package com.xj.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;


public class StreamUtil {
	/**
	 * 把输入流的内容输出到输出流中
	 * @param is  输入流
	 * @param os  输出流
	 * @return
	 */
	public static boolean is2os(InputStream is, OutputStream os) {

		ReadableByteChannel inChannel = Channels.newChannel(is);
		WritableByteChannel outChannel = Channels.newChannel(os);

		ByteBuffer buffer = ByteBuffer.allocate(65536);
		try {
			while (inChannel.read(buffer) != -1) {
				buffer.flip();
				outChannel.write(buffer);
				buffer.clear();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			close(inChannel);
			close(outChannel);
			close(is);
			close(os);
		}
		return true;
	}

	/**
	 * 把输入流的内容输出到输出流中,输出完后不关闭流
	 * @param is  输入流
	 * @param os  输出流
	 * @return
	 */
	public static boolean is2osNoClose(InputStream is, OutputStream os) {

		ReadableByteChannel inChannel = Channels.newChannel(is);
		WritableByteChannel outChannel = Channels.newChannel(os);

		ByteBuffer buffer = ByteBuffer.allocate(65536);
		try {
			while (inChannel.read(buffer) != -1) {
				buffer.flip();
				outChannel.write(buffer);
				buffer.clear();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 关闭资源
	 * @param closeable
	 */
	public static void close(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
				System.err.println("关闭流出现异常");
				e.printStackTrace();
			}
		}
		closeable = null;
	}

	/**
	 * 读取length个字节
	 * @param is
	 * @param length
	 * @return
	 * @throws IOException
	 */
	public static byte[] getBytes(InputStream is, final int length)		throws IOException {
		if(length <=  0){
			return null ;
		}
		int totalLength = length;
		byte[] buf = null ;
		ByteArrayOutputStream bos = null ;
		if (totalLength > 0) {
			bos = new ByteArrayOutputStream();
			int totalRead = 0;
			//int readAttempt = 0;
			int len = -1;
			//最多用4K做缓存
			if(totalLength > 4096){
				buf = new byte[4096];
			}else{
				buf = new byte[totalLength];
			}
			while (totalRead < totalLength) {
//				readAttempt = totalLength - totalRead;
//				// 如果努力读取的字节数大于buf数组长度，则努力读取buf数组长度就够了
//				if (readAttempt > buf.length) {
//					readAttempt = buf.length;
//				}
				len = is.read(buf);
				totalRead += len;
				bos.write(buf, 0, len);
			}
		}
		return bos.toByteArray();
	}
	
	
	public static void buf2File(String filename,byte[] buf) throws IOException{
		File file = new File(filename);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		bos.write(buf);
		bos.flush();
		bos.close();
//		fos.close();
	}

}
